<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          @click="translation"
        >打印</el-button>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column label="编号" prop="number"></el-table-column>
      <el-table-column label="数量" prop="num"></el-table-column>
      <el-table-column label="申请人" prop="createUserName"></el-table-column>
      <el-table-column label="备注" prop="remark"></el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="apply_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button style="margin-right: 10px;" link type="primary" size="small" v-if="scope.row.state === 0 || scope.row.state === 3" @click="handleSub(scope.row)">提交</el-button>
          <el-button style="margin-right: 10px;" link type="primary" size="small" v-if="scope.row.state === 0 || scope.row.state === 3" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.state === 0 || scope.row.state === 3" size="small" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="paging.pageNum"
      :limit.sync="paging.pageSize"
      @pagination="onPageChange"
    />
    <el-dialog

      v-model="isRestDialogVisible"
      width="50%"
      @close="handleDialogClose"
    >
      <rest-dialog ref="RestDialog" @sub="getList"></rest-dialog>
    </el-dialog>
    <edit ref="edit" @sub="onSub"></edit>
  </div>
</template>

<script>
import api from "./api.js";
import { getCurrentInstance } from 'vue';
import Edit from './edit.vue';


export default {
  name: "AssetInApply",
  components: {
    Edit,
    RestDialog
  },
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      list: [],
      title: "",
      open: false,
      paging: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        configName: null,
        configDesc: null,
        configType: null,
        orderType: null,
        status: null,
        createUserId: null,
        updateUserId: null
      },
      form: {},
      isRestDialogVisible: false,
      apply_state: []
    };
  },
  setup() {
    const { proxy } = getCurrentInstance();
    const { apply_state } = proxy.useDict("apply_state");
    return {
      apply_state
    };
  },
  methods: {
    getList() {
      this.loading = true;
      const { pageNum, pageSize } = this.paging;
      const query = { ...this.queryParams };
      const pageReq = { page: pageNum - 1, size: pageSize };
      api.list(query, pageReq).then(response => {
        const { content, totalElements } = response;
        this.list = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    onPageChange({ page, limit }) {
      this.paging.pageNum = page;
      this.paging.pageSize = limit;
      this.getList();
    },
    translation() {
      this.isRestDialogVisible = true;
    },
    handleDialogClose() {
      this.isRestDialogVisible = false;
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        configName: null,
        configDesc: null,
        configType: null,
        orderType: null,
        status: 0,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.$refs.edit.open({
        actionType: 'add'
      });
    },
    handleUpdate(row) {
      this.$refs.edit.open({
        actionType: 'edit',
        row: row
      });
    },
    handleSub(row) {
      api.sub(row.id).then(() => {
        this.getList();
      });
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除').then(function() {
        return api.del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    onSub() {
      this.getList();
    }
  }
};
</script>
